package com.ruoyi.hcrf.controller.agv;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.hcrf.bo.agv.TaskAgvBo;
import com.ruoyi.hcrf.bo.agv.TaskAgvInsert;
import com.ruoyi.hcrf.bo.agv.TaskAgvLocation;
import com.ruoyi.hcrf.bo.agv.TaskAgvReturn;
import com.ruoyi.hcrf.domain.StockFlow;
import com.ruoyi.hcrf.domain.StockMaster;
import com.ruoyi.hcrf.domain.entity.task.AgvMonious;
import com.ruoyi.hcrf.domain.entity.task.InOut;
import com.ruoyi.hcrf.domain.entity.task.InventoryRecordLog;
import com.ruoyi.hcrf.domain.entity.task.TaskAgv;
import com.ruoyi.hcrf.domain.service.agv.InOutService;
import com.ruoyi.hcrf.domain.service.agv.InventoryRecordLogService;
import com.ruoyi.hcrf.domain.service.agv.TaskAgvService;
import com.ruoyi.hcrf.domain.service.storage.StockFlowLogService;
import com.ruoyi.hcrf.domain.service.storage.StorageMasterService;
import com.ruoyi.hcrf.enums.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@Api(tags = "RDS接口调用")
@RestController
@RequestMapping("/agv/task-agv")
public class TaskAgvController {

    @Autowired
    private TaskAgvService taskAgvService;

    @Autowired
    private StockFlowLogService stockFlowLogService;

    @Autowired
    private StorageMasterService storageMasterService;

    @Autowired
    private InOutService inOutService;

    @Autowired
    private InventoryRecordLogService inventoryRecordLogService;


    public TaskAgvController(StockFlowLogService stockFlowLogService) {
        this.stockFlowLogService = stockFlowLogService;
    }

    @ApiOperation(value = "RDS+AGF---任务生成接口")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public CommonResult rdsSelect(@RequestBody TaskAgvBo taskAgvBo) {

        TaskAgvReturn agvReturn = new TaskAgvReturn();
        String uuid = UUID.randomUUID().toString();
        String taskId = uuid.replaceAll("-", "").substring(0, 16);
        TaskAgvInsert taskAgvInsert = new TaskAgvInsert();
        try {
            //起点临时保管室-终点生药卸货室
            if (taskAgvBo.getType() == 1) {
                //获取取货点位--先查询在库表数据
                StockFlow stockFlow = taskAgvService.selectFlow();
//            StockMaster stockMaster = taskAgvService.select(stockFlow.getLocationId());
                //获取放货点位
                StockMaster master = taskAgvService.selectTo();
                if (stockFlow != null && master != null) {
                    //取货点位
                    agvReturn.setFromSite(stockFlow.getLocationId());
                    //放货点位
                    agvReturn.setToSite(master.getLocationId());
                    //生成Job_NFO=表位：agv任务表
//                String boxId = taskAgvBo.getBoxId() != null ? taskAgvBo.getBoxId() : null;
                    //判断开捆箱id是否为空，不为空则赋值
                    setTaskAgvInsertProperties(taskAgvInsert, taskId, stockFlow.getLocationId(),
                            master.getLocationId(), stockFlow.getStockId(),
                            master.getLocationId(), stockFlow.getBoxId());

                    //新增agv任务表
                    boolean a = taskAgvService.insert(taskAgvInsert);
                    if (a) {
                        agvReturn.setTaskId(taskId);
                        return CommonResult.success(agvReturn);
                    } else {
                        return CommonResult.error("新增失败");
                    }
                }
            }
            //起点生药卸货室 -- 终点裁切室
            if (taskAgvBo.getType() == 2) {
                //开捆箱id
                if (taskAgvBo.getBoxId() != null && taskAgvBo.getToSite() != null) {
                    //根据查询在库表数据
                    StockFlow stockFlow = taskAgvService.selectById(taskAgvBo.getBoxId());
                    if (stockFlow != null) {
                        //根据在库库位数据与库位数据匹配
                        StockMaster stockMaster = taskAgvService.select(stockFlow.getLocationId());
                        if (stockMaster != null) {
                            //取货点位
                            agvReturn.setFromSite(stockMaster.getLocationId());
                            //放货点位
                            agvReturn.setToSite(taskAgvBo.getToSite());
                            //生成Job_NFO=表位：agv任务表
                            String boxId = taskAgvBo.getBoxId() != null ? taskAgvBo.getBoxId() : null;
                            //判断开捆箱id是否为空，不为空则赋值
                            setTaskAgvInsertProperties(taskAgvInsert, taskId, stockMaster.getLocationId(), taskAgvBo.getToSite(), stockFlow.getStockId(), stockMaster.getLocationId(), boxId);
                            //新增agv任务表
                            boolean a = taskAgvService.insert(taskAgvInsert);
                            if (a) {
                                agvReturn.setTaskId(taskId);
                                return CommonResult.success(agvReturn);
                            } else {
                                return CommonResult.error("新增失败");
                            }
                        }
                        return CommonResult.error("库位不存在：" + stockFlow.getLocationId());
                    }
                }
                return CommonResult.error("条件不满足：" + taskAgvBo.getBoxId() + taskAgvBo.getToSite());
            }
            //起点裁切室 -- 终点生药卸货室
            if (taskAgvBo.getType() == 3) {
                //  开捆箱id+fromSite
                //计算库位
                if (taskAgvBo.getFromSite() != null) {
                    StockMaster stockMaster = taskAgvService.selectTo();
                    if (stockMaster != null) {
                        //有货位
                        agvReturn.setFromSite(taskAgvBo.getFromSite());
                        //放货点位
                        agvReturn.setToSite(stockMaster.getLocationId());
                        //生成Job_NFO=表位：agv任务表
                        String boxId = taskAgvBo.getBoxId() != null ? taskAgvBo.getBoxId() : null;
                        //判断开捆箱id是否为空，不为空则赋值
                        setTaskAgvInsertProperties(taskAgvInsert, taskId, taskAgvBo.getFromSite(), stockMaster.getLocationId(), null, stockMaster.getLocationId(), boxId);
                        //新增agv任务表
                        boolean a = taskAgvService.insert(taskAgvInsert);
                        if (a) {
                            agvReturn.setTaskId(taskId);
                            return CommonResult.success(agvReturn);
                        } else {
                            return CommonResult.error("任务新增失败");
                        }
                    }
                    return CommonResult.error("无库位");
                }
                return CommonResult.error("条件不满足：" + taskAgvBo.getFromSite());
            }
            //起点生药卸货室 -- 终点临时保管室
            if (taskAgvBo.getType() == 4) {
                //fromSite
                //查询生药卸货室库位信息
                StockFlow stockFlow = taskAgvService.selectFlowTwo();
//            StockMaster stockMaster = taskAgvService.select(stockFlow.getLocationId());
                //获取放货点位
                StockMaster master = taskAgvService.selectToTwo();
                if (stockFlow != null && master != null) {
                    //取货点位
                    agvReturn.setFromSite(stockFlow.getLocationId());
                    //放货点位
                    agvReturn.setToSite(master.getLocationId());
                    //生成Job_NFO=表位：agv任务表
                    String boxId = taskAgvBo.getBoxId() != null ? taskAgvBo.getBoxId() : null;
                    //判断开捆箱id是否为空，不为空则赋值
                    setTaskAgvInsertProperties(taskAgvInsert, taskId, stockFlow.getLocationId(), master.getLocationId(), stockFlow.getStockId(), stockFlow.getLocationId(), stockFlow.getBoxId());

                    //新增agv任务表
                    boolean a = taskAgvService.insert(taskAgvInsert);
                    if (a) {
                        agvReturn.setTaskId(taskId);
                        return CommonResult.success(agvReturn);
                    } else {
                        return CommonResult.error("新增失败");
                    }
                }
                return CommonResult.error("数据为空：" + stockFlow.getLocationId() + master.getLocationId());
            }
            //起点卸货室 -- 终点临时保管室
            //根据出入库指示书--
            if (taskAgvBo.getType() == 5) {

                //根据出入库指示书查询在库表数据
                LambdaQueryWrapper<InOut> inOutWrapper = Wrappers.lambdaQuery(InOut.class);
                inOutWrapper.eq(InOut::getResult, 3).eq(InOut::getStatus, 0).orderByAsc(InOut::getBoxId).last("limit 1");
                InOut inOut = inOutService.getOne(inOutWrapper);
                if (inOut != null) {
                    //根据在库表数据查询库位信息
                    StockFlow stockFlow = taskAgvService.selectById(inOut.getBoxId());
                    if (stockFlow != null) {
                        //说明出库数据--与在库数据匹配
                        //则获取放货点位
                        StockMaster master = taskAgvService.selectToTwo();
                        if (master != null) {
                            //取货点位
                            agvReturn.setFromSite(stockFlow.getLocationId());
                            //放货点位
                            agvReturn.setToSite(master.getLocationId());
                            //生成Job_NFO=表位：agv任务表
                            String boxId = inOut.getBoxId() != null ? inOut.getBoxId() : null;
                            //判断开捆箱id是否为空，不为空则赋值
                            setTaskAgvInsertProperties(taskAgvInsert, taskId, stockFlow.getLocationId(), master.getLocationId(), stockFlow.getStockId(), stockFlow.getLocationId(), boxId);
                            //新增agv任务表
                            boolean a = taskAgvService.insert(taskAgvInsert);
                            if (a) {
                                agvReturn.setTaskId(taskId);
                                return CommonResult.success(agvReturn);
                            } else {
                                return CommonResult.error("新增失败");
                            }
                        }
                    }
                    return CommonResult.error("在库数据不存在");
                }
            }
        } catch (Exception e) {
            System.out.println("出现异常: " + taskAgvBo.getType());
        }
        return CommonResult.error("数据不对");
    }

    @ApiOperation(value = "RDS+AGF+AGV---已接单接口")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public CommonResult updateById(@RequestBody TaskAgvBo taskAgvBo) {

        //代表已接单
        if (taskAgvBo.getStatus() == 1 && taskAgvBo.getTaskId() != null) {
            //修改agv任务
            Boolean a = taskAgvService.findById(taskAgvBo.getTaskId());
            if (a) {
                return CommonResult.success();
            }
        }
        return CommonResult.error("修改失败");
    }


    @ApiOperation(value = "RDS+AGF---任务完成接口")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult rdsUpdate(@RequestBody TaskAgvBo taskAgvBo) {
        try {
            //代表已接单--并且是保管室
            if (taskAgvBo.getStatus() == 2 && taskAgvBo.getTaskId() != null && taskAgvBo.getType() == 1) {
                //修改agv任务状态已完成
                Boolean a = taskAgvService.findByIdOk(taskAgvBo.getTaskId());
                if (a) {
                    return CommonResult.success();
                }
            }
            if (taskAgvBo.getStatus() == 2 && taskAgvBo.getTaskId() != null && taskAgvBo.getType() == 2) {
                //修改agv任务状态已完成
                Boolean a = taskAgvService.findByIdOkTwo(taskAgvBo.getTaskId());
                if (a) {
                    return CommonResult.success();
                }
            }
            //特殊处理---裁切室任务完成状态
            if (taskAgvBo.getStatus() == 2 && taskAgvBo.getTaskId() != null && taskAgvBo.getType() == 3) {
                if (taskAgvBo.getResult() == 1 || taskAgvBo.getResult() == 3) {
                    //是空箱 或 退货
                    //修改agv任务状态已完成
                    Boolean a = taskAgvService.findByIdOkThree(taskAgvBo);
                    if (a) {
                        return CommonResult.success();
                    }
                }
                return CommonResult.error("数据为空：" + taskAgvBo.getResult());
            }
            if (taskAgvBo.getStatus() == 2 && taskAgvBo.getTaskId() != null && taskAgvBo.getType() == 4) {
                //修改agv任务状态已完成
                Boolean a = taskAgvService.findByIdOkFour(taskAgvBo.getTaskId());
                if (a) {
                    return CommonResult.success();
                }
            }

            if (taskAgvBo.getStatus() == 2 && taskAgvBo.getTaskId() != null && taskAgvBo.getType() == 5) {
                //修改agv任务状态已完成
                LambdaQueryWrapper<TaskAgv> taskAgvWrapper = Wrappers.lambdaQuery(TaskAgv.class);
                taskAgvWrapper.eq(TaskAgv::getTaskId, taskAgvBo.getTaskId());
                TaskAgv taskAgv = taskAgvService.getOne(taskAgvWrapper);
                taskAgv.setStatus(2);
                boolean b = taskAgvService.updateById(taskAgv);
                if (b) {
                    //删除在库数据卸货室数据-逻辑删
                    LambdaQueryWrapper<StockFlow> stockFlowWrapper = Wrappers.lambdaQuery(StockFlow.class);
                    stockFlowWrapper.eq(StockFlow::getStockId, taskAgv.getFlowId()).eq(StockFlow::getDelFlag, 0);
                    StockFlow stockFlow = stockFlowLogService.getOne(stockFlowWrapper);
                    if (stockFlow != null) {
                        //修改出入库指示书数据
                        LambdaQueryWrapper<InOut> inOutWrapper = Wrappers.lambdaQuery(InOut.class);
                        inOutWrapper.eq(InOut::getResult, 3).eq(InOut::getStatus, 0)
                                .eq(InOut::getBoxId, stockFlow.getBoxId()).last("limit 1");
                        InOut inOut = inOutService.getOne(inOutWrapper);
                        if (inOut != null) {
                            inOut.setStatus(1);
                            inOut.setUpdateTime(new Date());
                            boolean b2 = inOutService.updateById(inOut);
                            if (b2) {
                                //逻辑删除在库数据
                                stockFlow.setDelFlag(1);
                                stockFlow.setInOutId(inOut.getId());
                                boolean c = stockFlowLogService.updateById(stockFlow);
                                if (c) {
                                    // //将库位状态改为未占用
                                    LambdaQueryWrapper<StockMaster> stockMaster = Wrappers.lambdaQuery(StockMaster.class);
                                    stockMaster.eq(StockMaster::getLocationId, taskAgv.getFromSite());
                                    StockMaster stock = storageMasterService.getOne(stockMaster);
                                    if (stock != null) {
                                        //将库位状态改为未占用
                                        stock.setStorageEnabled(0);
                                        stock.setStorageStatus(0);
                                        storageMasterService.updateById(stock);
                                        //生成出入库履历表
                                        saveInventoryRecordLog(stockFlow.getStockFlowType(),
                                                taskAgv.getBoxId(), stockFlow.getItemName(),
                                                taskAgv.getFromSite(), stockFlow.getActualQuantity());
                                        ////新增临时保管区 在库 信息
                                        StockFlow savedStockFlow = saveStockFlow
                                                (taskAgv.getBoxId(), stockFlow.getItemName(),
                                                        taskAgv.getToSite(), 1,
                                                        stockFlow.getActualQuantity(), stockFlow.getStockFlowType(), inOut.getId());
                                        if (savedStockFlow != null) {
                                            //锁库位
                                            //将库位状态改为已占用
                                            LambdaQueryWrapper<StockMaster> master = Wrappers.lambdaQuery(StockMaster.class);
                                            master.eq(StockMaster::getLocationId, taskAgv.getToSite());
                                            StockMaster stockMaster1 = storageMasterService.getOne(master);
                                            if (stockMaster1 != null) {
                                                stockMaster1.setStorageEnabled(1);
                                                stockMaster1.setStorageStatus(stockFlow.getStockFlowType());
                                                boolean b1 = storageMasterService.updateById(stockMaster1);
                                                if (b1) {
                                                    //出入库履历
                                                    saveInventoryRecordLog(stockFlow.getStockFlowType(), taskAgv.getBoxId(), stockFlow.getItemName(), taskAgv.getToSite(), stockFlow.getActualQuantity());
                                                }
                                                return CommonResult.error("库位状态修改失败");
                                            }
                                        }
                                    }
                                }
                            }
                            return CommonResult.error("修改失败");
                        }
                        return CommonResult.error("出入库指示书数据不存在");
                    }
                }
                return CommonResult.error("修改失败");
            }
        } catch (Exception e) {
            System.out.println("出现异常: " + taskAgvBo.getType() + taskAgvBo.getStatus() + taskAgvBo.getTaskId() + taskAgvBo.getResult());
        }
        return CommonResult.error("" + taskAgvBo.getStatus() + taskAgvBo.getType() + taskAgvBo.getTaskId() + taskAgvBo.getResult());
    }

    @ApiOperation(value = "RDS+AGF---出库照合接口")
    @RequestMapping(value = "/selectBoxId", method = RequestMethod.POST)
    public CommonResult selectBoxId(@RequestBody TaskAgvBo taskAgvBo) {
        TaskAgvLocation location = new TaskAgvLocation();
        try {
            if (taskAgvBo.getBoxId() != null) {
                //根据起点位-终点位
                InOut inOut = taskAgvService.selectBoxId(taskAgvBo.getBoxId());
                if (inOut != null) {
                    if (inOut.getResult() == 1) {
                        StockMaster stockMaster = taskAgvService.selectToTwo();
                        if (stockMaster != null) {
                            //生成在库数据
                            stockMaster.setStorageStatus(2);
                            stockMaster.setStorageEnabled(1);
                            boolean b = storageMasterService.updateById(stockMaster);

                            //放货库位
                            location.setToSite(stockMaster.getLocationId());

                            if (b) {
                                //生成在库数据
                                StockFlow stockFlow = new StockFlow();
                                stockFlow.setStockId(UUID.randomUUID().toString());
                                //出入库指示书id
                                stockFlow.setInOutId(inOut.getId());
                                //开捆箱id
                                stockFlow.setBoxId(inOut.getBoxId());
                                //库区
                                stockFlow.setWareHouse(stockMaster.getWareHouse());
                                stockFlow.setStatus(0);
                                //库位
                                stockFlow.setLocationId(stockMaster.getLocationId());
                                //在库类型
                                stockFlow.setStockFlowType(stockMaster.getStorageStatus());
                                stockFlow.setActualQuantity(inOut.getQty());
                                stockFlow.setItemName(inOut.getItemName());
                                stockFlow.setDelFlag(0);
                                stockFlow.setStockTime(new Date());
                                stockFlow.setCreateTime(new Date());
                                boolean a = stockFlowLogService.save(stockFlow);
                                if (a) {
                                    //修改出入库指示书数据
                                    inOut.setStatus(1);
                                    inOut.setUpdateTime(new Date());
                                    boolean b1 = inOutService.updateById(inOut);
                                    if (b1) {
                                        //货物类型
                                        location.setType(stockMaster.getStorageStatus());
                                        return CommonResult.success(location);
                                    }
                                }
                            }
                        }
                    }
                    if (inOut.getResult() == 3) {
                        //查询在库数据是退货数据
                        LambdaQueryWrapper<StockFlow> stockFlowLambdaQueryWrapper = Wrappers.lambdaQuery(StockFlow.class);
                        stockFlowLambdaQueryWrapper.eq(StockFlow::getBoxId, inOut.getBoxId()).eq(StockFlow::getDelFlag, 0);
                        StockFlow flowLogServiceOne = stockFlowLogService.getOne(stockFlowLambdaQueryWrapper);
                        flowLogServiceOne.setDelFlag(1);
                        flowLogServiceOne.setInOutId(inOut.getId());
                        boolean c = stockFlowLogService.updateById(flowLogServiceOne);
                        if (c) {
                            //修改库位状态
                            LambdaQueryWrapper<StockMaster> stockMasterLambdaQueryWrapper = Wrappers.lambdaQuery(StockMaster.class);
                            stockMasterLambdaQueryWrapper.eq(StockMaster::getLocationId, flowLogServiceOne.getLocationId());
                            StockMaster stockMaster = storageMasterService.getOne(stockMasterLambdaQueryWrapper);
                            stockMaster.setStorageEnabled(0);
                            stockMaster.setStorageStatus(0);
                            boolean b = storageMasterService.updateById(stockMaster);
                            if (b) {
                                //库位修改完成
                                //生成出入库履历表
                                saveInventoryRecordLog(flowLogServiceOne.getStockFlowType(),
                                        flowLogServiceOne.getBoxId(), flowLogServiceOne.getItemName(),
                                        flowLogServiceOne.getLocationId(), flowLogServiceOne.getActualQuantity());
                                //修改出入库指示书
                                inOut.setStatus(2);
                                inOut.setUpdateTime(new Date());
                                boolean b1 = inOutService.updateById(inOut);
                                if (b1) {
                                    //货物类型
                                    location.setType(flowLogServiceOne.getStockFlowType());
                                    //库位
                                    location.setToSite(flowLogServiceOne.getLocationId());
                                    return CommonResult.success(location);
                                }
                            }
                        }
                    }
                    //获取临时保管室的库位信息
                }
                return CommonResult.error("数据不存在");
            }
        } catch (Exception e) {
            System.out.println("出现异常: " + taskAgvBo.getBoxId());
        }
        return CommonResult.error("参数为空：" + taskAgvBo.getBoxId());
    }

    @ApiOperation(value = "RDS+AGF---库位+在库修改接口")
    @RequestMapping(value = "/rdsUpdateLocation", method = RequestMethod.POST)
    public CommonResult rdsUpdateLocation(@RequestBody TaskAgvBo taskAgvBo) {

        try {
            if (taskAgvBo.getFromSite() != null && taskAgvBo.getToSite() != null) {
                //根据起点位-终点位
                boolean aboolean = taskAgvService.updateLocation(taskAgvBo);
                if (aboolean) {
                    return CommonResult.success();
                }
            }
        } catch (Exception e) {
            System.out.println("出现异常: " + taskAgvBo.getBoxId());
        }

        return CommonResult.error("参数为空：" + taskAgvBo.getFromSite() + taskAgvBo.getToSite());
    }


    @ApiOperation(value = "RDS+AGF---库位禁用接口")
    @RequestMapping(value = "/updateDisable", method = RequestMethod.POST)
    public CommonResult updateDisable(@RequestBody TaskAgvBo taskAgvBo) {
        try {
            //库位与禁用状态词不能为空
            if (taskAgvBo.getFromSite() != null && taskAgvBo.getResult() != null) {
                //根据起点位-终点位
                LambdaQueryWrapper<StockMaster> stockMasterLambdaQueryWrapper = Wrappers.lambdaQuery(StockMaster.class);
                stockMasterLambdaQueryWrapper.eq(StockMaster::getLocationId, taskAgvBo.getFromSite());
                StockMaster stockMaster = storageMasterService.getOne(stockMasterLambdaQueryWrapper);
                stockMaster.setStorageEnabled(taskAgvBo.getResult());
                boolean b = storageMasterService.updateById(stockMaster);
                if (b) {
                    return CommonResult.success("禁用成功");
                }
            }
        } catch (Exception e) {
            System.out.println("出现异常: " + taskAgvBo.getFromSite());
        }
        return CommonResult.error("参数为空：" + taskAgvBo.getFromSite() + taskAgvBo.getResult());
    }

    @ApiOperation(value = "RDS+AGF---空箱逻辑处理接口")
    @RequestMapping(value = "/updateLocation", method = RequestMethod.POST)
    public CommonResult updateLocation(@RequestBody TaskAgvBo taskAgvBo) {
        //库位不可为空
        if (taskAgvBo.getFromSite() != null) {
            //根据库位查询在库数据中的空箱数据
            LambdaQueryWrapper<StockFlow> stockFlowWrapper = Wrappers.lambdaQuery(StockFlow.class);
            stockFlowWrapper.eq(StockFlow::getLocationId, taskAgvBo.getFromSite()).eq(StockFlow::getDelFlag, 0);
            StockFlow stockFlow = stockFlowLogService.getOne(stockFlowWrapper);
            if (stockFlow != null) {
                stockFlow.setStatus(1);
                stockFlow.setDelFlag(1);
                boolean b = stockFlowLogService.updateById(stockFlow);
                if (b) {
                    //修改库位占用状态
                    LambdaQueryWrapper<StockMaster> stockMaster = Wrappers.lambdaQuery(StockMaster.class);
                    stockMaster.eq(StockMaster::getLocationId, taskAgvBo.getFromSite());
                    StockMaster stock = storageMasterService.getOne(stockMaster);
                    stock.setStorageEnabled(0);
                    stock.setStorageStatus(0);
                    boolean b1 = storageMasterService.updateById(stock);
                    if (b1) {
                        //生成出入库履历
                        saveInventoryRecordLog(stockFlow.getStockFlowType(),
                                stockFlow.getBoxId(), stockFlow.getItemName(),
                                taskAgvBo.getFromSite(), stockFlow.getActualQuantity());
                        return CommonResult.success("库存修改成功");
                    }
                }
            }
            return CommonResult.error("库存无数据" + stockFlow);
        }
        return CommonResult.error("库位不存在：" + taskAgvBo.getFromSite());
    }

    @ApiOperation(value = "RDS+AGV---调和容器任务生成接口")
    @RequestMapping(value = "/agvInsert", method = RequestMethod.POST)
    public CommonResult agvInsert(@RequestBody TaskAgvBo taskAgvBo) {
        TaskAgvReturn agvReturn = new TaskAgvReturn();
        String uuid = UUID.randomUUID().toString();
        String taskId = uuid.replaceAll("-", "").substring(0, 16);
        TaskAgvInsert taskAgvInsert = new TaskAgvInsert();
        //1-入库任务type   2-空桶出库to+type    3-满桶出库to+boxid+type
        try {
            //1-入库任务type
            if (taskAgvBo.getType() == 1 && taskAgvBo.getFromSite() != null) {
                //查询调和容器库位信息
                StockMaster master = taskAgvService.selectAgv();
                if (master != null) {
                    //取货点位
                    agvReturn.setFromSite(taskAgvBo.getFromSite());
                    //放货点位
                    agvReturn.setToSite(master.getLocationId());
                    setTaskAgvInsertProperties(taskAgvInsert, taskId, taskAgvBo.getFromSite(), master.getLocationId(),
                            null, master.getLocationId(), null);

                    //新增agv任务表
                    boolean a = taskAgvService.insert(taskAgvInsert);
                    if (a) {
                        agvReturn.setTaskId(taskId);
                        return CommonResult.success(agvReturn);
                    } else {
                        return CommonResult.error("新增失败");
                    }
                }
            }
            //2-空桶出库to+type
            if (taskAgvBo.getType() == 2 && taskAgvBo.getToSite() != null) {

                //查询调和容器在库空桶数据
                AgvMonious agvMonious = taskAgvService.selectAgvNull();
                if (agvMonious != null) {
                    //取货点位
                    agvReturn.setFromSite(agvMonious.getLocationId());
                    //放货点位
                    agvReturn.setToSite(taskAgvBo.getToSite());

                    setTaskAgvInsertProperties(taskAgvInsert, taskId,
                            agvMonious.getLocationId(), taskAgvBo.getToSite(),
                            agvMonious.getId(), taskAgvBo.getToSite(), agvMonious.getItemNo());

                    //新增agv任务表
                    boolean a = taskAgvService.insert(taskAgvInsert);
                    if (a) {
                        agvReturn.setTaskId(taskId);
                        return CommonResult.success(agvReturn);
                    } else {
                        return CommonResult.error("新增失败");
                    }
                }
                return CommonResult.error("空桶数据为空" + agvMonious);
            }

            if (taskAgvBo.getType() == 3 && taskAgvBo.getToSite() != null && taskAgvBo.getBoxId() != null) {
                //查询在库满桶数据
                AgvMonious agvMonious = taskAgvService.selectFlowById(taskAgvBo.getBoxId());
                if (agvMonious != null) {
                    //取货点位
                    agvReturn.setFromSite(agvMonious.getLocationId());
                    //放货点位
                    agvReturn.setToSite(taskAgvBo.getToSite());
                    //生成Job_NFO=表位：agv任务表
                    String boxId = taskAgvBo.getBoxId() != null ? taskAgvBo.getBoxId() : null;
                    //判断开捆箱id是否为空，不为空则赋值
                    setTaskAgvInsertProperties(taskAgvInsert, taskId, agvMonious.getLocationId(),
                            taskAgvBo.getToSite(), agvMonious.getId(), taskAgvBo.getToSite(), boxId);
                    //新增agv任务表
                    boolean a = taskAgvService.insert(taskAgvInsert);
                    if (a) {
                        agvReturn.setTaskId(taskId);
                        return CommonResult.success(agvReturn);
                    } else {
                        return CommonResult.error("新增失败");
                    }
                }
                return CommonResult.error("满桶数据为空" + agvMonious);
            }
        } catch (Exception e) {
            System.out.println("出现异常: " + taskAgvBo.getType());
        }

        return CommonResult.error("数据为空：" + taskAgvBo.getType());
    }


    @ApiOperation(value = "RDS+AGV---调和容器任务完成接口")
    @RequestMapping(value = "/agvUpdate", method = RequestMethod.POST)
    public CommonResult agvUpdate(@RequestBody TaskAgvBo taskAgvBo) {
        try {
            //入库任务完成
            if (taskAgvBo.getStatus() == 2 && taskAgvBo.getTaskId() != null && taskAgvBo.getType() == 1) {
                if (taskAgvBo.getResult() == 1 || taskAgvBo.getResult() == 2) {
                    //是空箱 或 ，满桶
                    Boolean a = taskAgvService.findAgv(taskAgvBo);
                    if (a) {
                        return CommonResult.success();
                    }
                }
                return CommonResult.error("数据为空：" + taskAgvBo.getResult());
            }
            //空桶出库任务完成
            if (taskAgvBo.getStatus() == 2 && taskAgvBo.getTaskId() != null && (taskAgvBo.getType() == 2 || taskAgvBo.getType() == 3)) {
                Boolean a = taskAgvService.findByIdAGv(taskAgvBo.getTaskId());
                if (a) {
                    return CommonResult.success();
                }
            }
        } catch (Exception e) {
            System.out.println("出现异常: " + taskAgvBo.getType());
        }
        //满桶出库任务完成
        return CommonResult.error("数据为空：" + taskAgvBo.getTaskId());
    }

    //设置TaskAgvInsert属性
    public void setTaskAgvInsertProperties(TaskAgvInsert taskAgvInsert, String taskId, String locationGoods, String goods, String stockId, String locationId, String boxId) {
        taskAgvInsert.setTaskId(taskId);
        taskAgvInsert.setFromSite(locationGoods);
        taskAgvInsert.setToSite(goods);
        taskAgvInsert.setBoxId(boxId);
        taskAgvInsert.setLocationId(locationId);
        taskAgvInsert.setStockId(stockId);
    }

    //出入库履历新增
    public void saveInventoryRecordLog(Integer storageStatus, String boxId, String itemName, String fromSite, double stockQty) {
        // 创建并设置 InventoryRecordLog
        InventoryRecordLog inventoryRecordLog = new InventoryRecordLog();
        inventoryRecordLog.setId(UUID.randomUUID().toString());
        inventoryRecordLog.setFlowType(storageStatus); // 出库流向
        inventoryRecordLog.setBoxId(boxId); // 箱子ID
        inventoryRecordLog.setItemName(itemName); // 商品名称
        inventoryRecordLog.setWareHouse(2); // 卸货室
        inventoryRecordLog.setLocationId(fromSite); // 位置ID
        inventoryRecordLog.setQty(stockQty); // 库存数量
        inventoryRecordLog.setCreateTime(new Date()); // 创建时间
        // 保存到数据库
        inventoryRecordLogService.save(inventoryRecordLog);
    }

    //临时保管室新增数据
    public StockFlow saveStockFlow(String boxId, String itemName, String locationId, int wareHouse, double stockQty, Integer storageStatus, String id) {
        // 创建并设置 StockFlow
        StockFlow stockFlow = new StockFlow();
        stockFlow.setStockId(UUID.randomUUID().toString()); // 生成唯一的库存ID
        //出入库指示书id
        stockFlow.setInOutId(id);
        stockFlow.setBoxId(boxId); // 箱子ID
        stockFlow.setItemName(itemName); // 商品名称
        stockFlow.setLocationId(locationId); // 位置ID
        stockFlow.setWareHouse(wareHouse); // 仓库编号，1为入库
        stockFlow.setActualQuantity(stockQty); // 库存数量
        stockFlow.setStatus(0); // 设置状态
        stockFlow.setStockFlowType(storageStatus); // 出库或入库的流向
        stockFlow.setStockTime(new Date()); // 库存时间
        stockFlow.setCreateTime(new Date()); // 创建时间

        // 保存到数据库
        stockFlowLogService.save(stockFlow);

        // 返回保存后的 StockFlow 对象
        return stockFlow;
    }


//    @ApiOperation(value = "新增库位数据")
//    @RequestMapping(value = "/inaaa", method = RequestMethod.POST)
//    public void in(@RequestParam("id") String id,
//                   @RequestParam("locationGoods") String locationGoods,
//                   @RequestParam("wareHoust") Integer wareHoust,
//                   @RequestParam("i") Integer i) {
//        for (Integer integer = 1; integer <= i; integer++) {
//            StockMaster stockMaster = new StockMaster();
//            stockMaster.setStorageEnabled(2);
//            stockMaster.setStorageStatus(0);
//            stockMaster.setLocationId(id + locationGoods);
//            stockMaster.setLocationGoods(locationGoods);
//            stockMaster.setWareHouse(wareHoust);
//            stockMaster.setLayers(String.valueOf(integer));
//            stockMaster.setCreateTime(new Date());
//            stockMaster.setCreateBy("1");
//
//            storageMasterService.save(stockMaster);
//
//        }
//    }
}
